# leetcode 142 环形链表II

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        access = {head: 1}
        if not head:
            return None
        curr = head.next
        while curr:
            if curr in access:
                return curr
            else:
                access[curr] = 1
            curr = curr.next
        return None
